package com.csjbot.blackgaga;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.android.core.util.StrUtil;
import com.csjbot.blackgaga.base.BaseFullScreenActivity;
import com.csjbot.blackgaga.base.BasePresenter;
import com.csjbot.blackgaga.global.Constants;
import com.csjbot.blackgaga.home.HomeActivity;
import com.csjbot.blackgaga.model.http.ApiUrl;
import com.csjbot.blackgaga.network.NetworkListenerService;
import com.csjbot.blackgaga.util.SharedKey;
import com.csjbot.blackgaga.util.SharedPreUtil;
import com.csjbot.blackgaga.util.SharedPreferencesSDUtil;

import java.io.File;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

import static com.csjbot.blackgaga.R.id.bt_chinese;

public class MainActivity extends BaseFullScreenActivity {

    @BindView(R.id.bt_english)
    Button mBtEnglish;
    @BindView(R.id.bt_chinese)
    Button mBtChinese;
    @BindView(R.id.bt_japanese)
    Button mBtJapanese;
    private Button lastClick;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public boolean isOpenTitle() {
        return true;
    }

    /**
     * 是否停留在当前页面
     */
    public static final String STAY = "stay";

    @Override
    public void init() {
        super.init();
        if(!getIntent().getBooleanExtra(STAY,false)) {
            isGoHome();
        }
        mMode = SharedPreUtil.getInt(SharedKey.LANGUAGEMODE_NAME, SharedKey.LANGUAGEMODE_KEY, Constants.Language.CHINESE);
        showLanguage(mMode, false);
        if (BaseApplication.isFirstComing) {
            BaseApplication.isFirstComing = false;
            recreate();
        }
    }

    private void isGoHome(){
        int i = SharedPreUtil.getInt(SharedKey.LANGUAGEMODE_NAME, SharedKey.LANGUAGEMODE_KEY,-1);
        if(i != -1){
            jumpActivity(HomeActivity.class);
            this.finish();
        }
    }

    @OnClick(bt_chinese)
    public void chinese() {
        selectBgChange(mBtChinese);
        showLanguage(Constants.Language.CHINESE, true);
        SharedPreUtil.putInt(SharedKey.LANGUAGEMODE_NAME, SharedKey.LANGUAGEMODE_KEY, Constants.Language.CHINESE);
        jumpActivity(HomeActivity.class);
        finish();
    }

    private void selectBgChange(Button bt) {
        lastClick.setBackgroundResource(R.drawable.main_bt_normal);
        lastClick.setTextColor(getResources().getColor(R.color.grey_999999));
        bt.setBackgroundResource(R.drawable.main_bt_pressed);
        bt.setTextColor(getResources().getColor(R.color.msyh_press));
        lastClick = bt;
    }

    @Override
    public void afterViewCreated(Bundle savedInstanceState) {
        getTitleView().setLanguageVisibility(View.GONE);

        startService(new Intent(MainActivity.this, NetworkListenerService.class));
//        startService(new Intent(MainActivity.this, CheckEthernetService.class));

        checkWifi();
    }


    private void selectBgChange(int mode) {
        switch (mode) {
            case 0:
                mBtEnglish.setBackgroundResource(R.drawable.main_bt_pressed);
                mBtEnglish.setTextColor(getResources().getColor(R.color.msyh_press));
                lastClick = mBtEnglish;
                break;
            case 1:
                mBtChinese.setBackgroundResource(R.drawable.main_bt_pressed);
                mBtChinese.setTextColor(getResources().getColor(R.color.msyh_press));
                lastClick = mBtChinese;
                break;

            case 2:
                mBtJapanese.setBackgroundResource(R.drawable.main_bt_pressed);
                mBtJapanese.setTextColor(getResources().getColor(R.color.msyh_press));
                lastClick = mBtJapanese;
                break;
            default:
                break;
        }

    }


    @Override
    public boolean isOpenChat() {
        return true;
    }

    @OnClick({R.id.bt_english, R.id.bt_japanese})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_english:
                selectBgChange(mBtEnglish);
                showLanguage(Constants.Language.ENGELISH, true);
                SharedPreUtil.putInt(SharedKey.LANGUAGEMODE_NAME, SharedKey.LANGUAGEMODE_KEY, Constants.Language.ENGELISH);
                jumpActivity(HomeActivity.class);
                finish();
                break;
            case R.id.bt_japanese:
                selectBgChange(mBtJapanese);
                showLanguage(Constants.Language.JPANESE, true);
                SharedPreUtil.putInt(SharedKey.LANGUAGEMODE_NAME, SharedKey.LANGUAGEMODE_KEY, Constants.Language.JPANESE);
                jumpActivity(HomeActivity.class);
                finish();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        urlInit();
        selectBgChange(mMode);
        String urlUtil = (String) SharedPreferencesSDUtil.get(this, "urlUtil", SharedKey.DEFAULT_ADRESS, "");
        if (TextUtils.isEmpty(urlUtil)) {
            urlUtil = (String) SharedPreferencesSDUtil.get(this, "urlUtil", SharedKey.TEST_ADRESS, "");

        }
//        ApiUrl.DEFAULT_ADRESS = urlUtil;
    }

    /**
     * 如果本地不存在接口文件就將阿裡云接口緩存在本地
     */
    private void urlInit() {
        if (!new File("/sdcard/blackgaga/data/urlUtil.xml").exists()) {
            if (StrUtil.isBlank((String) SharedPreferencesSDUtil.get(this, "urlUtil", SharedKey.DEFAULT_ADRESS, ""))) {
                SharedPreferencesSDUtil.put(this, "urlUtil", SharedKey.DEFAULT_ADRESS, ApiUrl.DEFAULT_ADRESS);
            }
        }

    }


    /**
     * @param language_mode
     * @param isFreash      刷新本地语言的方法
     */
    public void showLanguage(int language_mode, boolean isFreash) {
        Configuration config = context.getResources().getConfiguration();
        switch (language_mode) {
            case 0:
                config.locale = Locale.UK;
                break;
            case 1:
                config.locale = Locale.CHINA;
                break;
            case 2:
                config.locale = Locale.JAPAN;
                break;

        }
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        if (isFreash) {
            refresh();
        }
    }

    /**
     * 刷新界面
     */
    private void refresh() {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    /**
     * 检查Wifi如果是关闭状态则开启Wifi
     */
    public void checkWifi() {
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        int state = wifiManager.getWifiState();
        if (state == WifiManager.WIFI_STATE_DISABLED) {
            wifiManager.setWifiEnabled(true);
        }
    }

}
